<template>
    <div>
        <!------用户管理查询按钮------>
        <div class="search">
            <el-input v-model="name" placeholder="请输入姓名" suffix-icon="el-icon-search" style="width: 220px;"
                      @keyup.enter.native="loadPost">
            </el-input>

            <el-select v-model="userIdentity" filterable placeholder="请选择身份" style="margin-left: 5px;">
                <el-option
                        v-for="item in userIdentitys"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>

            <el-button type="primary" style="margin-left: 10px;" @click="loadPost">查询</el-button>
            <el-button type="warning" @click="resetParam">重置</el-button>
            <el-button type="success" style="margin-left: 10px;" @click="checkUser">审核</el-button>
        </div>

        <!------信息展示列表------>
        <el-table :data="tableData"
                  :header-cell-style="{color:'#c71111'}"
                  @current-change="selectCurrentChange"
                  highlight-current-row>
<!--            <el-table-column prop="id" label="ID" width="50">
            </el-table-column>-->
            <el-table-column type="index" label="序号" width="50">
            </el-table-column>

            <el-table-column prop="studentId" label="学号" width="160" sortable>
            </el-table-column>

            <el-table-column prop="name" label="姓名" width="120">
            </el-table-column>

            <el-table-column prop="userIdentity" label="身份" width="120">
                <template slot-scope="scope">
                    <el-tag
                            :type="scope.row.userIdentity === 6 ? 'danger' : scope.row.userIdentity === 5 ? 'warning' :scope.row.userIdentity === 4 ? 'success' : scope.row.userIdentity === 3 ? 'primary' : 'info'"
                            disable-transitions>{{scope.row.userIdentity === 6?'中共党员'
                        :(scope.row.userIdentity===5?'中共预备党员'
                        :(scope.row.userIdentity===4?'发展对象'
                        :(scope.row.userIdentity===3?'积极分子'
                        :'入党成员')))}}
                    </el-tag>
                </template>
            </el-table-column>

            <el-table-column prop="approver" label="审批人" width="120">
            </el-table-column>

            <el-table-column prop="organization" label="组织" width="200" :formatter="formatOrganizations">
            </el-table-column>

            <el-table-column prop="createtime" label="申请时间" width="200" sortable>
            </el-table-column>

            <el-table-column prop="download" label="文件" width="160">
                <template slot-scope="scope">
                    <el-button type="primary" v-if="scope.row.fileUrl" size="small"
                               @click="window.open(scope.row.fileUrl)" :disabled="scope.row.downFile">
                        下载申请文件
                    </el-button>
                </template>
            </el-table-column>

            <el-table-column prop="statu" label="状态">
                <template slot-scope="scope">
                    <el-tag
                            :type="scope.row.statu == 1 ? 'primary' : scope.row.statu == 2 ? 'success' : 'danger'"
                            disable-transitions>{{scope.row.statu == 1 ? '待审批' : scope.row.statu == 2 ? '已通过' :
                        '未通过'}}
                    </el-tag>
                </template>
            </el-table-column>
        </el-table>


        <!------分页查询------>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[5, 9, 18, 30]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>


        <!------审批列表------>
        <el-dialog
                title="入党申请"
                :visible.sync="centerDialogVisible"
                width="30%"
                center>

            <el-form ref="form" :rules="rules" :model="form" label-width="80px">

                <el-form-item label="学号" prop="studentId">
                    <el-col :span="20">
                        <el-input v-model="form.studentId" readonly></el-input>
                    </el-col>
                </el-form-item>

                <el-form-item label="姓名" prop="name">
                    <el-col :span="20">
                        <el-input v-model="form.name" readonly></el-input>
                    </el-col>
                </el-form-item>

                <el-form-item label="身份" prop="userIdentity">
                    <el-col :span="20">
                        <el-input v-model="userIdentityText" readonly>
                        </el-input>
                    </el-col>
                </el-form-item>

                <el-form-item label="组织" prop="organization">
                    <el-col :span="20">
                        <el-select v-model="form.organization" disabled style="margin-left: 5px;">
                            <el-option
                                    v-for="item in organizationsData"
                                    :key="item.code"
                                    :label="item.name"
                                    :value="item.code">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>

                <el-form-item label="申请时间" prop="createtime">
                    <el-col :span="20">
                        <el-date-picker
                                v-model="form.createtime"
                                type="datetime"
                                placeholder="选择日期时间" disabled>
                        </el-date-picker>
                    </el-col>
                </el-form-item>

            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button type="success" @click="pass">通过</el-button>
                <el-button type="danger" @click="refuse">拒绝</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {serverIp} from "../../../public/config";

    export default {
        name: "DevelpManage",
        data() {
            return {
                serverIp:serverIp,
                downFile: false,
                window: window,
                users: JSON.parse(sessionStorage.getItem('CurUser')),
                organizationsData: [],
                tableData: [],
                pageSize: 9,
                pageNum: 1,
                total: 0,
                currentRow: {},
                name: '',
                userIdentity: '',
                userIdentitys: [
                    {
                        value: '5',
                        label: '中共预备党员'
                    }, {
                        value: '4',
                        label: '发展对象'
                    }, {
                        value: '3',
                        label: '积极分子'
                    }, {
                        value: '2',
                        label: '入党成员'
                    }
                ],

                centerDialogVisible: false,
                form: {
                    id: '',
                    studentId: '',
                    name: '',
                    userIdentity: '',
                    approver: '',
                    organization: '',
                    createtime: '',
                    statu: '',
                    fileUrl: '',
                },
                rules: {}

            };
        },
        computed: {
            userIdentityText() {
                switch (this.form.userIdentity) {
                    case 6:
                        return '中共党员';
                    case 5:
                        return '中共预备党员';
                    case 4:
                        return '发展对象';
                    case 3:
                        return '积极';
                    default:
                        return '入党成员';
                }
            },
        },

        created() {
            // 初始化downFile属性
            this.tableData.forEach(item => {
                if (!item.fileUrl) { // 如果当前行的fileUrl为空，则禁用下载按钮
                    item.downFile = true
                }
            })
        },

        methods: {
            downFailUrl() {
                if (!this.form.fileUrl) {
                    this.downFile = true;
                }
            },
            // download(url) {
            //     window.location.href = url; // 页面跳转到url链接所在的资源，触发下载操作
            // },
            download(rowData) {
                // 禁用当前行的下载按钮
                rowData.downFile = true
                // 下载文件
                window.location.href = rowData.fileUrl
            },

            pass() {
                this.form.statu = 2;
                this.form.approver = this.users.name;
                this.$axios.post('http://'+ serverIp +':8090/apply/update', this.form)
                    .then(res => res.data).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$message({
                            message: '操作成功！',
                            type: 'success'
                        });
                        this.centerDialogVisible = false;
                        this.loadPost();
                        this.resetForm()
                    } else {
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    }
                })
            },
            refuse() {
                this.form.statu = 3;
                this.form.approver = this.users.name;
                this.$axios.post('http://'+ serverIp +':8090/apply/refuse', this.form)
                    .then(res => res.data).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$message({
                            message: '操作成功！',
                            type: 'success'
                        });
                        this.centerDialogVisible = false;
                        this.loadPost();
                        this.resetForm()
                    } else {
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    }
                })

            },
            userIdentityLabel() {
                let item = this.userIdentitys.find((u) => u.value === this.form.userIdentity);
                return item ? item.label : '';
            },
            selectCurrentChange(val) {
                this.currentRow = val;
                if (this.currentRow.statu != 1) {
                    alert('不能选择已审批的记录');
                    this.currentRow = {};
                    return;
                }
            },
            checkUser() {
                if (!this.currentRow.id) {
                    alert('请选择记录');
                    return;
                }
                this.centerDialogVisible = true
                this.$nextTick(() => {
                    this.resetForm()
                });
                this.form.id = this.currentRow.id;
                this.form.studentId = this.currentRow.studentId;
                this.form.name = this.currentRow.name;
                this.form.userIdentity = this.currentRow.userIdentity;
                this.form.organization = this.currentRow.organization;
                this.form.createtime = this.currentRow.createtime;
                this.form.fileUrl = this.currentRow.fileUrl;
            },
            formatOrganizations(row) {
                let temp = this.organizationsData.find(item => {
                    return item.code == row.organization
                });

                return temp && temp.name
            },

            resetForm() {
                this.$refs.form.resetFields();
            },

            resetParam() {
                this.name = '';
                this.userIdentity = '';
                this.loadPost()
            },

            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageNum = 1;
                this.pageSize = val;
                this.loadPost()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.pageNum = val;
                this.loadPost()
            },

            loadGet() {
                this.$axios.get('http://'+ serverIp +':8090/apply/list').then(res => res.data).then(res => {
                    console.log(res)
                })
            },

            loadPost() {
                const users = JSON.parse(sessionStorage.getItem('CurUser'));
                this.$axios.post('http://'+ serverIp +':8090/apply/page', {
                    pageSize: this.pageSize,
                    pageNum: this.pageNum,

                    param: {
                        name: this.name,
                        userIdentity: this.userIdentity,
                        organization: users.organization,
                    }
                }).then(res => res.data).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.tableData = res.data
                        this.total = res.total
                    } else {
                        alert('获取数据失败')
                    }

                })
            },
            loadOrganizations() {
                this.$axios.get('http://'+ serverIp +':8090/organizations/list').then(res => res.data).then(res => {
                    console.log(res)
                    if (res.code == 200) {
                        this.organizationsData = res.data
                    } else {
                        alert('获取数据失败')
                    }

                })
            }
        },

        beforeMount() {
            // this.loadGet();
            this.loadPost();
            this.loadOrganizations();
            // this.downFailUrl();

        }
    }
</script>

<style scoped>

    .search {
        margin-bottom: 5px;
    }
</style>